package com.hghivln.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.constants.Constants;
import com.hghivln.mapper.RoleMapper;
import com.hghivln.mapper.UserMapper;
import com.hghivln.pojo.domain.User;
import com.hghivln.pojo.dto.UserDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.UserService;
import com.hghivln.utils.AppMd5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public User queryUserByPhone(String phone) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq(User.COL_PHONE,phone);
        qw.eq(User.COL_DEL_FLAG,0);
        return userMapper.selectOne(qw);
    }

    @Override
    public AjaxResult listUserForPage(UserDto userDto) {
        Page<User> page = new Page<>(userDto.getPageNum(),userDto.getPageSize());
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq(userDto.getDeptId() != null,User.COL_DEPT_ID,userDto.getDeptId());
        qw.like(StringUtils.isNotBlank(userDto.getUserName()),User.COL_USER_NAME,userDto.getUserName());
        qw.like(StringUtils.isNotBlank(userDto.getPhone()),User.COL_PHONE,userDto.getPhone());
        qw.eq(userDto.getStatus() != null,User.COL_STATUS,userDto.getStatus());

        qw.ge(userDto.getBeginTime() != null,User.COL_CREATE_TIME,userDto.getBeginTime());
        qw.le(userDto.getEndTime() != null,User.COL_CREATE_TIME,userDto.getEndTime());
        qw.eq(User.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.orderByAsc(User.COL_USER_ID);
        qw.select("user_id","dept_id","user_name","user_type","sex","age","picture","background","phone","email","strong","honor","introduction","user_rank","status","create_time","scheduling_flag");

        userMapper.selectPage(page,qw);
        return AjaxResult.success("查询成功",page.getRecords(),page.getTotal());
    }

    @Override
    public AjaxResult addUser(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        user.setUserType(Constants.USER_NORMAL);
        user.setCreateBy(userDto.getSimpleUser().getUserName());
        user.setCreateTime(DateUtil.date());
        user.setSalt(AppMd5Utils.createSalt());
        String defaultPwd = user.getPhone().substring(5);
        user.setPassword(AppMd5Utils.md5(defaultPwd,user.getSalt(),2));
        return AjaxResult.success(userMapper.insert(user));
    }

    @Override
    public AjaxResult updateUser(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        user.setUpdateBy(userDto.getSimpleUser().getUserName());
        return AjaxResult.success(userMapper.updateById(user));
    }

    @Override
    public AjaxResult deleteUserByIds(Long[] ids) {
        try {
            List<Long> idList = Arrays.asList(ids);
            roleMapper.deleteRoleUserByUserIds(idList);
            return AjaxResult.success(userMapper.deleteUserByIds(idList));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    @Override
    public AjaxResult getUserById(Long id) {
        try {
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq(User.COL_DEL_FLAG,Constants.DEL_FALSE);
            qw.eq(User.COL_USER_ID,id);
            qw.select("user_id","dept_id","user_name","user_type","sex","age","picture","background","phone","email","strong","honor","introduction","user_rank","status","create_time","scheduling_flag");
            return AjaxResult.success(userMapper.selectOne(qw));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public AjaxResult resetPwd(Long[] userIds) {
        for (Long userId : userIds) {
            User user = userMapper.selectById(userId);
            String defaultPwd = "";
            if (user.getUserType().equals(Constants.USER_ADMIN)) {
                defaultPwd = "123456";
            } else {
                defaultPwd = user.getPhone().substring(5);
            }
            user.setSalt(AppMd5Utils.createSalt());
            user.setPassword(AppMd5Utils.md5(defaultPwd,user.getSalt(),2));
            return AjaxResult.success("重置成功");
        }
        return AjaxResult.fail("重置失败");
    }

    @Override
    public AjaxResult selectAllUser() {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq(User.COL_STATUS,Constants.STATUS_TRUE);
        qw.eq(User.COL_USER_TYPE,Constants.USER_NORMAL);
        qw.orderByAsc(User.COL_USER_ID);
        return AjaxResult.success(userMapper.selectList(qw));
    }



}

